﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AutoLoL.Commands;

namespace AutoLoL.Tabs
{
    /// <summary>
    /// Interaction logic for ContactTab.xaml
    /// </summary>
    public partial class ContactTab : UserControl
    {
        public ContactTab()
        {
            InitializeComponent();
            DataContext = new ContactTabViewModel();
        }
    }

    public class ContactTabViewModel : DependencyObject
    {

        public string Email
        {
            get { return (string)GetValue(EmailProperty); }
            set { SetValue(EmailProperty, value); }
        }
        // Using a DependencyProperty as the backing store for Email.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EmailProperty =
            DependencyProperty.Register("Email", typeof(string), typeof(ContactTabViewModel));

        public string Subject
        {
            get { return (string)GetValue(SubjectProperty); }
            set { SetValue(SubjectProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Subject.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SubjectProperty =
            DependencyProperty.Register("Subject", typeof(string), typeof(ContactTabViewModel));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(ContactTabViewModel));

        public string ResultMessage
        {
            get { return (string)GetValue(ResultMessageProperty); }
            set { SetValue(ResultMessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ResultMessage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ResultMessageProperty =
            DependencyProperty.Register("ResultMessage", typeof(string), typeof(ContactTabViewModel));

        private ICommand _submitCommand;
        public ICommand SubmitCommand
        {
            get
            {
                if (_submitCommand == null)
                    _submitCommand = new RelayCommand(Submit, () => !string.IsNullOrEmpty(Email) && !string.IsNullOrEmpty(Message) && Message != defaultMessage);
                return _submitCommand;
            }
        }

        string defaultMessage = "Error is recurring (happens often):\r\n\r\n\r\nSteps to reproduce error:\r\n1.\r\n2.\r\n3.\r\n\r\nError message (if any):\r\n\r\n\r\n";

        public ContactTabViewModel()
        {
            ResetForm();
        }

        private void ResetForm()
        {
            Message = defaultMessage;
            Subject = string.Empty;
            Email = string.Empty;
        }

        private void Submit()
        {
            ResultMessage = string.Empty;
            try
            {
                //$passcode = $_REQUEST['passcode'];
                //$from = $_REQUEST['email'] ;
                //$subject = $_REQUEST['subject'] ;
                //$message =  trim($_REQUEST['message']);
                string postData = "passcode=" + HttpUtility.UrlEncode("xp#0-2Xldpo!0ckle0!@#ASX!@#LADC") +
                                  "&email=" + HttpUtility.UrlEncode(Email) +
                                  "&subject=" + HttpUtility.UrlEncode(Subject) +
                                  "&message=" + HttpUtility.UrlEncode(Message);

                string result = string.Empty;

                WebRequest req = WebRequest.Create("http://codingquirks.com/apps/autolol/BugEmail.php?" + postData);
                using (WebResponse resp = req.GetResponse())
                {
                    using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                    {
                        result = sr.ReadToEnd();
                    }
                }

                bool valid = bool.Parse(result.Substring(0, 5).Trim());
                ResultMessage = result.Substring(5).Trim();
                if (valid)
                {
                    ResultMessage += Environment.NewLine + "Thank you for your submission.";
                    ResetForm();
                }
            }
            catch (Exception ex)
            {
                ResultMessage =
                "This is really embarrassing. The bug submission form has a bug in it!!!, Email us directly for this one.\r\n Error: " + ex.ToString();
            }
        }
    }
}
